package com.example.demo.util;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtils {

    private final  static  String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    private  final  static  String ENCODE="utf-8";
    private  final  static  String SUFFIX=".xlsx";

    public static  void exportExcel(HttpServletResponse response, String fileName, List<?> data, Class<?> tClass, String sheetName) throws  Exception{
        response.setContentType(CONTENT_TYPE);
        response.setCharacterEncoding(ENCODE);
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName,ENCODE) + SUFFIX);
        EasyExcel.write(response.getOutputStream(),tClass).sheet(sheetName).doWrite(data);
    }
}
